//
//  ResumePreViewController.m
//  eshow
//
//  Created by Hisoka on 15/12/4.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "ResumePreViewController.h"
#import "WXApiRequestHandler.h"
#import "WXApiManager.h"
#import "MyViewController.h"
#import "MyResumeViewController.h"
#include <QuartzCore/QuartzCore.h>

@interface ResumePreViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ResumePreViewController

- (void) viewDidAppear:(BOOL)animated {
    if (_isPopRoot) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }else{
        if ([_vcName isEqualToString:@"myResuemVC"]) {
            MyResumeViewController *myResumeVC = [[MyResumeViewController alloc] init];
            [self.navigationController popToViewController:myResumeVC animated:YES];
        }else{
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self customizeBackItem];
    __weak typeof (self) weakSelf = self;
    [self customizeBackItemWithCallBack:^{
        if (_isPopRoot) {
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setStyle];
}

- (void) setStyle {
    self.view.backgroundColor = WHITE_COLOR;
    
    UIView *shareV = [UIView newAutoLayoutView];
    [self.view addSubview:shareV];
    
    [shareV autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [shareV autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [shareV autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 60)];
    
    UILabel *shareToLB = [UILabel newAutoLayoutView];
    [shareV addSubview:shareToLB];
    
    [shareToLB autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:shareV];
    [shareToLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:shareV];
    [shareToLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 20)];
    shareToLB.backgroundColor = GRAY_COLOR;
    shareToLB.text = @"  分享简历到";
    shareToLB.font = FONT_SIZE_16;
    shareToLB.textColor = [UIColor grayColor];
    
    UILabel *wechatMomentLB = [UILabel newAutoLayoutView];
    [shareV addSubview:wechatMomentLB];
    
    [wechatMomentLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:shareToLB];
    [wechatMomentLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:shareToLB];
    [wechatMomentLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth / 2, 40)];
    wechatMomentLB.backgroundColor = BG_BLACK_COLOR;
    wechatMomentLB.text = @"  微信朋友圈";
    wechatMomentLB.font = FONT_SIZE_16;
    wechatMomentLB.textAlignment = NSTextAlignmentCenter;
    wechatMomentLB.textColor = [UIColor grayColor];
    
    UIButton *wechatMomentBTN = [UIButton newAutoLayoutView];
    [shareV addSubview:wechatMomentBTN];
    
    [wechatMomentBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:wechatMomentLB];
    [wechatMomentBTN autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:wechatMomentLB];
    [wechatMomentBTN autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:wechatMomentLB];
    [wechatMomentBTN autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:wechatMomentLB];
    [wechatMomentBTN setBackgroundColor:[UIColor clearColor]];
    [wechatMomentBTN addTarget:self action:@selector(shareToWechatMoment) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *wechatMomentImg = [UIImageView newAutoLayoutView];
    [shareV addSubview:wechatMomentImg];
    
    [wechatMomentImg autoAlignAxis:ALAxisHorizontal toSameAxisOfView:wechatMomentLB];
    [wechatMomentImg autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:wechatMomentLB withOffset:20];
    [wechatMomentImg autoSetDimensionsToSize:CGSizeMake(24, 24)];
    wechatMomentImg.image = [UIImage imageNamed:@"wechatMomentICON"];
    
    UILabel *wechatFriendsLB = [UILabel newAutoLayoutView];
    [shareV addSubview:wechatFriendsLB];
    
    [wechatFriendsLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:shareToLB];
    [wechatFriendsLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:wechatMomentLB];
    [wechatFriendsLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth / 2, 40)];
    wechatFriendsLB.backgroundColor = BG_BLACK_COLOR;
    wechatFriendsLB.text = @"微信好友";
    wechatFriendsLB.font = FONT_SIZE_16;
    wechatFriendsLB.textAlignment = NSTextAlignmentCenter;
    wechatFriendsLB.textColor = [UIColor grayColor];
    
    UIButton *wechatFriendsBTN = [UIButton newAutoLayoutView];
    [shareV addSubview:wechatFriendsBTN];
    
    [wechatFriendsBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:wechatFriendsLB];
    [wechatFriendsBTN autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:wechatFriendsLB];
    [wechatFriendsBTN autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:wechatFriendsLB];
    [wechatFriendsBTN autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:wechatFriendsLB];
    [wechatFriendsBTN setBackgroundColor:[UIColor clearColor]];
    [wechatFriendsBTN addTarget:self action:@selector(shareToWechatFriends) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *wechatFriendsImg = [UIImageView newAutoLayoutView];
    [shareV addSubview:wechatFriendsImg];
    
    [wechatFriendsImg autoAlignAxis:ALAxisHorizontal toSameAxisOfView:wechatFriendsLB];
    [wechatFriendsImg autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:wechatFriendsLB withOffset:20];
    [wechatFriendsImg autoSetDimensionsToSize:CGSizeMake(24, 24)];
    wechatFriendsImg.image = [UIImage imageNamed:@"wechatFriendsICON"];
    
    UILabel *cutLB = [UILabel newAutoLayoutView];
    [shareV addSubview:cutLB];
    
    [cutLB autoAlignAxis:ALAxisHorizontal toSameAxisOfView:shareV];
    [cutLB autoAlignAxis:ALAxisVertical toSameAxisOfView:shareV];
    [cutLB autoSetDimensionsToSize:CGSizeMake(1, 60)];
    cutLB.backgroundColor = GRAY_COLOR;
    
    self.webView = [[UIWebView alloc] initForAutoLayout];
    [self.view addSubview:_webView];
    
    [_webView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:shareV];
    [_webView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_webView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    [_webView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    
    _webView.delegate = self;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_preViewUrl]];
    
    [_webView loadRequest:request];
    
    if (_hasShowMy) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的"
                                                                                  style:UIBarButtonItemStyleDone
                                                                                 target:self
                                                                                 action:@selector(pushToMyVC)];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    }

}

- (void) webViewDidFinishLoad:(UIWebView *)webView {
    
//    UIView *view = webView;
    
//    [self uploadScreen:_webView];
}

#pragma mark - 截屏
- (UIImage*)screenView:(UIView *)view{
    CGRect rect = view.frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

#pragma mark - 上传截屏图片
- (void) uploadScreen:(UIView *)view {
    
    UIImage *image = [self screenView:view];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[NSString stringWithFormat:@"%@", [[TMCache sharedCache] objectForKey:@"SSID"]] forKey:@"ssid"];
    [parameters setObject:@"resume" forKey:@"type"];
    [parameters setObject:_rid forKey:@"rid"];
    
    [[HttpManager instance] uploadFile:@"Util/uploadFile" imageData:image parameters:parameters success:^(NSDictionary *result) {

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

- (void) pushToMyVC {
    MyViewController *myVC = [[MyViewController alloc] init];
    myVC.isPopRoot = YES;
    [self.navigationController pushViewController:myVC animated:YES];
}

#pragma mark - 分享到朋友圈
- (void) shareToWechatMoment {
    [MobClick event:[NSString stringWithFormat:@"Click_shareBTN_pyq_%@", self.tid]];
    
    NSString *tmpTitle = [self getShareTitle:_shareTitle];
    
    [WXApiRequestHandler sendLinkURL:_preViewUrl
                             TagName:nil
                               Title:[NSString stringWithFormat:TEXT_SHARE_CONTENT, tmpTitle]
                         Description:[NSString stringWithFormat:TEXT_SHARE_CONTENT, tmpTitle]
                          ThumbImage:[self getImageFormUrl:_imageUrl]
                             InScene:WXSceneTimeline];
}

#pragma mark - 分享到微信好友
- (void) shareToWechatFriends {
    [MobClick event:[NSString stringWithFormat:@"Click_shareBTN_py_%@", self.tid]];

    NSString *tmpTitle = [self getShareTitle:_shareTitle];

    [WXApiRequestHandler sendLinkURL:_preViewUrl
                             TagName:nil
                               Title:tmpTitle
                         Description:[NSString stringWithFormat:TEXT_SHARE_CONTENT, tmpTitle]
                          ThumbImage:[self getImageFormUrl:_imageUrl]
                             InScene:WXSceneSession];
}

#pragma mark - 获取分享标题
- (NSString *) getShareTitle:(NSString *)title {
    
    if (title == nil || [title isEqualToString:@""]) return TEXT_SHARE_TITLE;
    
    return [NSString stringWithFormat:@"%@简历-e秀", title];
    
}

#pragma mark - 获取网络图片
- (UIImage *) getImageFormUrl:(NSString *) imgUrl {
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
    
    if (data == nil) return [UIImage imageNamed:@"logoOrangeBgIcon"];
    
    UIImage *res = [UIImage imageWithData:data];
    
    return res;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
